﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Data.EntityClient;


namespace dasFX.VSTime.API
{
    public class SQLSubmitEvent : ISubmitEvent, IDisposable
    {
        private EntityConnection _connection;
        private int _currentSolutionId;
        ISolutionRepository _solutionRepository;
        IEventRecordRepository _eventRepository;
        
        public SQLSubmitEvent(string connectionString, ISolutionRepository solutionRepository,
            IEventRecordRepository eventRecordRepository)
        {
            try
            {
                _solutionRepository = solutionRepository;
                _eventRepository = eventRecordRepository;
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to open VSTime database: " + ex.Message, ex);
            }
        }

        public static string BuildEntityConnectionString(string sqlConnection)
        {
            // Specify the provider name, server and database.
            string providerName = "System.Data.SqlClient";
            string serverName = ".";
            string databaseName = "AdventureWorks";

            // Initialize the connection string builder for the
            // underlying provider.
            SqlConnectionStringBuilder sqlBuilder =
                new SqlConnectionStringBuilder();

            // Set the properties for the data source.
            sqlBuilder.DataSource = @".\SQLEXPRESS";
            sqlBuilder.InitialCatalog = "VSTime";
            sqlBuilder.IntegratedSecurity = true;

            // Build the SqlConnection connection string.
            string providerString = sqlBuilder.ToString();

            // Initialize the EntityConnectionStringBuilder.
            EntityConnectionStringBuilder entityBuilder =
                new EntityConnectionStringBuilder();

            //Set the provider name.
            entityBuilder.Provider = providerName;

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = providerString;

            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/VSTimeModel.csdl|
                                        res://*/VSTimeModel.ssdl|
                                        res://*/VSTimeModel.msl";
            return entityBuilder.ToString();
        }

        #region ISubmitEvent Members

        public void RegisterEvent(string EventName, string EventDetail)
        {
            EventRecord record = new EventRecord();
            record.EventDate = System.DateTime.Now;
            record.EventDetail = EventDetail;
            record.EventName = EventName;
            record.SolutionID = _currentSolutionId;
            _eventRepository.Add(record);
            _eventRepository.Save();
        }      
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            _connection.Dispose();   
        }

        #endregion

        /// <summary>
        /// Registers a solution by creating new Solution or returning existing solution ID
        /// </summary>
        /// <param name="solutionPath"></param>
        /// <returns></returns>
        public int RegisterSolution(string solutionPath)
        {
            int solutionId = 0;
            string solutionFile = Path.GetFileName(solutionPath);
            
            //Register solution
            Solution solution = _solutionRepository.GetByFileName(solutionFile);
            if (solution != null)
            {
                solutionId = solution.ID;
            }
            else
            {
                //Insert new solution record
                solution = new Solution();
                solution.SolutionFileName = solutionFile;
                solution.SolutionFilePath = solutionPath;
                _solutionRepository.CreateSolution(solution);
                solutionId = solution.ID;
            }
            _currentSolutionId = solutionId;
            return solutionId;
        }

        public int SolutionID
        {
            get { return _currentSolutionId; }
        }
    }
}
